package com.xing.algorithm.array;

public class RotateImage {
    public static void rotate(int[][] matrix) {
        if(matrix == null) {
            return;
        }

        int i = 0;
        for(; i < matrix.length; i ++) {
            if(matrix[i].length != matrix.length) {
                return;
            }
        }

        i = 0;
        int n = matrix.length;
        for (; i < n / 2; ++i) {
            for (int j = i; j < n - 1 - i; ++j) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[n - 1 - j][i];
                matrix[n - 1 - j][i] = matrix[n - 1 - i][n - 1 - j];
                matrix[n - 1 - i][n - 1 - j] = matrix[j][n - 1 - i];
                matrix[j][n - 1 - i] = tmp;
            }
        }

    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{
                {5, 1, 9, 11},
                {2, 4, 8, 10},
                {13, 3, 6, 7},
                {15, 14, 12, 16}
        };

        rotate(matrix);

        for(int i = 0; i < matrix.length; i ++) {
            StringBuffer sb = new StringBuffer();
            sb.append("{");
            for(int j = 0; j < matrix[i].length; j ++) {
                sb.append(matrix[i][j]).append(",");
            }
            sb.append("}");

            System.out.println(sb.toString());
        }

    }
}
